class CardBean {
  static const int spades = 1; //黑桃
  static const int hearts = 2; //红心
  static const int clubs = 3; //梅花
  static const int diamonds = 4; //方片梅花
  int suit = 0; //1-黑桃,2-红心,3-,4-方片
  int num = 0; //牌值，其值范围为2..14, 赋予 2(deuce)=2, 3=3, ..., King=13, Ace=14
  bool select = false;

  CardBean(int suit, int num) {
    this.suit = suit;
    this.num = num;
  }

  String getValue() {
    String suitValue;
    String numValue;
    if (suit == spades) {
      suitValue = "♠";
    } else if (suit == hearts) {
      suitValue = "♥";
    } else if (suit == clubs) {
      suitValue = "♣";
    } else {
      suitValue = "♦";
    }
    if (num == 11) {
      numValue = "J";
    } else if (num == 12) {
      numValue = "Q";
    } else if (num == 13) {
      numValue = "K";
    } else if (num == 14) {
      numValue = "A";
    } else {
      numValue = num.toString();
    }
    return numValue + "  " + suitValue;
  }

  static String getCardValue(num, suit) {
    String suitValue;
    String numValue;
    if (suit == spades) {
      suitValue = "♠";
    } else if (suit == hearts) {
      suitValue = "♥";
    } else if (suit == clubs) {
      suitValue = "♣";
    } else {
      suitValue = "♦";
    }
    if (num == 11) {
      numValue = "J";
    } else if (num == 12) {
      numValue = "Q";
    } else if (num == 13) {
      numValue = "K";
    } else if (num == 14) {
      numValue = "A";
    } else {
      numValue = num.toString();
    }
    return numValue + " " + suitValue;
  }

  static List<CardBean> getAllCards() {
    List<CardBean> list = new List();
    for (var i = 1; i < 5; i++) {
      for (var j = 2; j < 15; j++) {
        list.add(new CardBean(i, j));
      }
    }
    return list;
  }

  static List<CardBean> str2Hands(String handsStr) {
    return [
      new CardBean(int.parse(handsStr.substring(1, 2)),
          getNumByStr(handsStr.substring(0, 1))),
      new CardBean(int.parse(handsStr.substring(3, 4)),
          getNumByStr(handsStr.substring(2, 3)))
    ];
  }

  ///根据范围字符串获取手牌的集合
  static List<String> getHandsStr(String range) {
    List<String> list = new List();
    String num1 = range.substring(0, 1);
    String num2 = range.substring(1, 2);
    if (range.endsWith("s")) {
      for (var i = 1; i < 5; i++) {
        list.add(num1 + i.toString() + num2 + i.toString());
      }
    } else if (range.endsWith("o")) {
      for (var i = 1; i < 5; i++) {
        for (var j = 1; j < 5; j++) {
          if (j != i) {
            list.add(num1 + i.toString() + num2 + j.toString());
          }
        }
      }
    } else {
      for (var i = 1; i < 4; i++) {
        for (var j = i + 1; j < 5; j++) {
          list.add(num1 + i.toString() + num2 + j.toString());
        }
      }
    }
    return list;
  }

  ///根据范围字符串获取手牌的集合
  static List<List<CardBean>> getRangeCards(String range) {
    List<List<CardBean>> list = new List();
    if (range.endsWith("s")) {
      for (var i = 1; i < 5; i++) {
        list.add([
          new CardBean(i, getNumByStr(range.substring(0, 1))),
          new CardBean(i, getNumByStr(range.substring(1, 2)))
        ]);
      }
    } else if (range.endsWith("o")) {
      for (var i = 1; i < 5; i++) {
        for (var j = 1; i < 5; j++) {
          if (j != i) {
            list.add([
              new CardBean(i, getNumByStr(range.substring(0, 1))),
              new CardBean(j, getNumByStr(range.substring(1, 2)))
            ]);
          }
        }
      }
    } else {
      for (var i = 1; i < 4; i++) {
        for (var j = i + 1; j < 5; j++) {
          list.add([
            new CardBean(i, getNumByStr(range.substring(0, 1))),
            new CardBean(j, getNumByStr(range.substring(1, 2)))
          ]);
        }
      }
    }
    return list;
  }

  static int getNumByStr(String numStr) {
    int num = 0;
    if (numStr == "T") {
      num = 10;
    } else if (numStr == "J") {
      num = 11;
    } else if (numStr == "Q") {
      num = 12;
    } else if (numStr == "K") {
      num = 13;
    } else if (numStr == "A") {
      num = 14;
    } else {
      num = int.parse(numStr);
    }
    return num;
  }

  static bool isSame(CardBean bean1, CardBean bean2) {
    return bean1.num == bean2.num && bean1.suit == bean2.suit;
  }
}
